//
//  MXTopView.m
//  MXの衣柜
//
//  Created by 马霄 on 15/8/5.
//  Copyright (c) 2015年 霄 马. All rights reserved.
//

#import "MXTopView.h"
#import "UIImageView+WebCache.h"
@interface MXTopView ()

@end


@implementation MXTopView

- (void)dealloc
{
    [_topViewArray release];
    [_mainScorllView release];
    [_cycleScrollView release];
    [_leftControl release];
    [_rightControl release];
    [_summaryLabel release];
    [_leftImage release];
    [_leftlabel release];
    [_rightImage release];
    [_rightlabel release];
    [_label release];
    [_LunboView release];
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.topViewArray = [NSMutableArray array];
        
#pragma mark - Label有个bug 需要添加text才能渲染
        self.label = [[[UILabel alloc] init] autorelease];
        self.label.text = @"";
        self.label.userInteractionEnabled = YES;
        self.label.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.label];
        
        self.cycleScrollView = [[[SDCycleScrollView alloc] init] autorelease];
        [self addSubview:self.cycleScrollView];
        
        self.LunboView = [[[UIView alloc] init] autorelease];
        self.LunboView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.LunboView];
        
        self.summaryLabel = [[[UILabel alloc] init] autorelease];
        self.summaryLabel.text = @"";
        self.summaryLabel.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:self.summaryLabel];
        
        
        self.leftlabel = [[[UILabel alloc] init] autorelease];
        self.leftlabel.userInteractionEnabled = YES;
        [self.label addSubview:self.leftlabel];
        
        self.leftImage = [[[UIImageView alloc] init] autorelease];
        self.leftImage.userInteractionEnabled = YES;
        [self.label addSubview:self.leftImage];
        
        self.rightImage = [[[UIImageView alloc] init] autorelease];
        self.rightImage.userInteractionEnabled = YES;
        [self.label addSubview:self.rightImage];
        
        self.rightlabel = [[[UILabel alloc] init] autorelease];
        self.rightlabel.userInteractionEnabled = YES;
        [self.label addSubview:self.rightlabel];
        
        
        self.leftControl = [[[UIControl alloc] init] autorelease];
        self.leftControl.backgroundColor = [UIColor clearColor];
        self.leftControl.alpha = 0.2;
        [self.label addSubview:self.leftControl];
        
        self.rightControl = [[[UIControl alloc] init] autorelease];
        self.rightControl.backgroundColor = [UIColor clearColor];
        self.rightControl.alpha = 0.2;
        [self.label addSubview:self.rightControl];
        
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat WID = WIDTH;
    
    CGFloat LunboX = 0;
    CGFloat LunboY = 0;
    CGFloat LunboW = WID;
    CGFloat LunboH = HEIGHT / 667 * 150;
    self.cycleScrollView.frame = CGRectMake(LunboX, LunboY, LunboW, LunboH);
    
    CGFloat LabelX = 0;
    CGFloat LabelY = LunboH;
    CGFloat LabelW = WID;
    CGFloat LabelH = HEIGHT / 667 * 40;
    self.label.frame = CGRectMake(LabelX, LabelY, LabelW, LabelH);
    
    CGFloat summarylabelX = 0;
    CGFloat summarylabelY = LabelH + LabelY;
    CGFloat summarylabelW = WID;
    CGFloat summarylabelH = HEIGHT / 667 * 30;
    self.summaryLabel.frame = CGRectMake(summarylabelX, summarylabelY, summarylabelW, summarylabelH);
    [self.summaryLabel setTextColor:[UIColor grayColor]];
    self.summaryLabel.textAlignment = NSTextAlignmentCenter;

    
//设置精选的Label
    CGFloat sumlabelX = WIDTH / 375 * 167;
    CGFloat sumlabelY = HEIGHT / 667 * 5;
    CGFloat sumlabelW = WIDTH / 375 * 40;
    CGFloat sumlabelH = HEIGHT / 667 * 20;
    UILabel *sumlabel = [[UILabel alloc] initWithFrame:CGRectMake(sumlabelX, sumlabelY, sumlabelW, sumlabelH)];
    sumlabel.backgroundColor = [UIColor clearColor];
    sumlabel.textAlignment = NSTextAlignmentCenter;
    sumlabel.text = @"精选";
    [sumlabel setTextColor:[UIColor grayColor]];
    [self.summaryLabel addSubview:sumlabel];
    
    
    UIView *line2 = [[[UIView alloc] initWithFrame:CGRectMake(WIDTH / 375 * 30, HEIGHT / 667 * 15, WIDTH / 375 * 130, 1)] autorelease];
    line2.backgroundColor = [UIColor grayColor];
    [self.summaryLabel addSubview:line2];
    
    
    UIView *line3 = [[[UIView alloc] initWithFrame:CGRectMake( WIDTH / 375 * 30 + WIDTH / 2, HEIGHT / 667 * 15, WIDTH / 375 * 130, 1)] autorelease];
    line3.backgroundColor = [UIColor grayColor];
    [self.summaryLabel addSubview:line3];

    
    
//设置首页的两个标签
#warning app的数据更新了 标签数增到了三个
    CGFloat imageX = WIDTH / 375 * 30;
    CGFloat imageY = 0;
    CGFloat imageW = self.label.frame.size.height;
    CGFloat imageH = self.label.frame.size.height;
    self.leftImage.frame = CGRectMake(imageX, imageY, imageW, imageH);
    self.leftImage.layer.cornerRadius = imageH / 2;
    self.leftlabel.layer.masksToBounds = YES;
    
    self.leftlabel.frame = CGRectMake(self.leftImage.center.x + WIDTH / 375 * imageW / 2, self.leftImage.center.y - HEIGHT / 667 * 10, WIDTH / 375 * 80, HEIGHT / 667 * 20);
    
    self.leftControl.frame = CGRectMake(imageX, imageY, imageW + HEIGHT / 667 * 80, imageH);
    
    
    UIView *line = [[[UIView alloc] initWithFrame:CGRectMake(WIDTH / 375 * 172, HEIGHT / 667 * 19.5, WIDTH / 375 * 30, 1)] autorelease];
    line.transform = CGAffineTransformRotate(line.transform, M_PI_2);
    line.backgroundColor = [UIColor lightGrayColor];
    [self.label addSubview:line];    
    
    CGFloat rightimageX = line.center.x + WIDTH / 375 * imageX;
    CGFloat rightimageY = 0;
    CGFloat rightimageW = self.label.frame.size.height;
    CGFloat rightimageH = self.label.frame.size.height;
    self.rightImage.frame = CGRectMake(rightimageX, rightimageY, rightimageW, rightimageH);
    self.rightImage.layer.cornerRadius = imageH / 2;
    self.rightImage.layer.masksToBounds = YES;
    
    self.rightControl.frame = CGRectMake(rightimageX, rightimageY, rightimageW + WIDTH / 375 * 80, rightimageH);
    
    
    self.rightlabel.frame = CGRectMake(self.rightImage.center.x + WIDTH / 375 * imageW / 2, self.rightImage.center.y - HEIGHT / 667 * 10, WIDTH / 375 * 80, HEIGHT / 667 * 20);
}
/*
-(void)setCycleScrollView:(SDCycleScrollView *)cycleScrollView
{
        NSMutableArray *viewsArray = [@[] mutableCopy];
    
        for (int i = 0; i < self.topViewArray.count; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHTPRO * 150)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.topViewArray[i]] placeholderImage:[UIImage imageNamed:@"plaseholder"]];
            [viewsArray addObject:imageView];
        }
    
        self.mainScorllView = [[CycleScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHTPRO * 150)];
    
        self.mainScorllView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.1];
        self.mainScorllView.fetchContentViewAtIndex = ^UIView *(NSInteger pageIndex){
            return viewsArray[pageIndex];
        };
    
        self.mainScorllView.totalPagesCount = ^NSInteger(void){
            return self.topViewArray.count;
        };
        self.mainScorllView.TapActionBlock = ^(NSInteger pageIndex){
            NSLog(@"点击了第%ld个", (long)pageIndex);
        };

}
*/

-(void)setMainScorllView:(CycleScrollView *)mainScorllView
{
    
    NSMutableArray *viewsArray = [@[] mutableCopy];

    for (int i = 0; i < self.topViewArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHTPRO * 150)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.topViewArray[i]] placeholderImage:[UIImage imageNamed:@"plaseholder"]];
        [viewsArray addObject:imageView];
    }
    
    self.mainScorllView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.1];
    
    self.mainScorllView.fetchContentViewAtIndex = ^UIView *(NSInteger pageIndex){
        return viewsArray[pageIndex];
    };
    
    self.mainScorllView.totalPagesCount = ^NSInteger (void){
        return self.topViewArray.count;
    };
    
    self.mainScorllView.TapActionBlock = ^(NSInteger pageIndex){
        NSLog(@"%ld", (long)pageIndex);
    };
    
    
    
}


@end
